package cn.zswltech.gruul.common.result;

import cn.zswltech.gruul.common.entity.OrgDO;
import cn.zswltech.gruul.common.enums.StatusCodeEnum;
import cn.zswltech.gruul.common.exception.BaseErrorEnum;
import cn.zswltech.gruul.common.exception.BaseException;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code;
    private String msg;
    private boolean success;
    private T data;

    public Response() {
    }

    public Response(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Response(int code, String msg, boolean success) {
        this.code = code;
        this.msg = msg;
        this.success = success;
    }

    /**
     * 通用成功
     *
     * @param data
     * @return
     */
    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<T>();
        response.data = data;
        response.setMessage(MSG.biz_success);
        response.success = true;
        return response;
    }

    public static <T> Response<T> success(){
        Response response = new Response();
        response.setMessage(MSG.biz_success);
        response.success = true;
        return response;
    }

    /**
     * 通用成功
     *
     * @param data
     * @return
     */
    public static <T> Response<T> success(T data, MSG message) {
        Response<T> response = new Response<T>();
        response.data = data;
        response.setMessage(message);
        response.success = true;
        return response;
    }

    /**
     * 通用成功
     *
     * @param data
     * @return
     */
    public static <T> Response<T> success(T data, int code) {
        Response<T> response = new Response<T>();
        response.data = data;
        response.setMessage(MSG.biz_success);
        response.success = true;
        response.setCode(code);
        return response;
    }

    /**
     * 通用失败异常
     *
     * @param statusCode
     * @param message
     * @param <T>
     * @return
     */
    public static <T> Response<T> error(StatusCodeEnum statusCode, String message) {
        return new Response<T>(statusCode.getStatus(), message == null ? statusCode.getMsg() : message);
    }

    public static <T> Response<T> error(BaseErrorEnum errorEnum){
        return new Response<T>(errorEnum.getErrCode(),errorEnum.getErrMsg());
    }


    public static <T> Response<T> error(BaseException baseException){
        return new Response<T>(baseException.getErrCode(),baseException.getErrMsg());
    }

    public static <T> Response<T> error(int status, String message) {
        return new Response<T>(status, message);
    }

    public static <T> Response<T> error(MSG msg, String... replacements) {
        Response<T> response = new Response<T>();
        response.setMessage(msg,replacements);
        return response;
    }

    public static <T> Response<T> errorTrue(MSG msg, String... replacements) {
        return new Response<T>(msg.code(), msg.getMsg(), true);
    }


    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setMessage(MSG msg, String... replacements) {
        if (replacements.length == 0){
            this.msg = msg.getMsg();
        }else {
            this.msg = msg.getMsg() + ":" + Arrays.toString(replacements);
        }
        this.code = msg.code();
        this.success = false;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
